<?php


namespace app\zhongcan\controller;
use app\zhongcan\model\BannerModel;
use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController
{
    public function index(){
        $data = BannerModel::select();
        $list=BannerModel::order('list_order','desc')
            ->paginate(4);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add(){
        return  $this->fetch();
    }

    public function addPost()
    {
        //接收数据
        $data = input('post.');
        $image = cmf_get_image_url($data['image']);
        if(BannerModel::create($data)){
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }

    public function edit()
    {
        $id = input('id');
        $new = BannerModel::where('id', '=', $id)
            ->find();
        $content1 = htmlspecialchars_decode($new['content']);
        $content2 = cmf_replace_content_file_url($content1);
        $new['content'] = $content2;
        $this->assign('news', $new);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $content1=htmlspecialchars_decode($data['content']);
        $content2=cmf_replace_content_file_url($content1,true);
        $data['content']=$content2;
        $v=new NewsValidate();
        $res=$v->scene('edit')->check($data);
        if(!$res){
            $this->error($v->getError());
        }
        $model=BannerModel::find($data['id']);

        $model->title=$data['title'];
        $model->image=$data['image'];
        if($model->save()){
            $this->success('修改成功','Banner/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=BannerModel::destroy($id);
        if($model){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }
    public function px(){
        $model = new BannerModel;
        parent::listOrders($model);
        $this->success('排序成功');
    }
}